package com.owen.server.impl;

import com.owen.dao.VegetablesDao;
import com.owen.pojo.Vegetables;
import com.owen.server.VegetablesServer;
import com.owen.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VegetablesServerImpl implements VegetablesServer {

    @Autowired
    private VegetablesDao vegetablesDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Vegetables> findAll() {
        List<Vegetables> vegetables = (List<Vegetables>)redisUtil.get("findVegetables");
        if(vegetables == null){
            vegetables = vegetablesDao.findAll();
            redisUtil.set("findVegetables", vegetables);
        }

        return vegetables;
    }

    @Override
    public List<Vegetables> findByVe(Vegetables vegetables) {
        return vegetablesDao.findByVe(vegetables);
    }

    @Override
    public Vegetables findById(Integer id) {
        return vegetablesDao.findById(id);
    }

    @Override
    public Integer addVegetables(Vegetables vegetables) {
        Integer integer = vegetablesDao.addVegetables(vegetables);
        redisUtil.del("findVegetables");
        return integer;
    }

    @Override
    public Integer deleteById(Integer id) {
        Integer integer = vegetablesDao.deleteById(id);
        redisUtil.del("findVegetables");
        return integer;
    }

    @Override
    public Integer update(Vegetables vegetables) {
        Integer integer = vegetablesDao.update(vegetables);
        redisUtil.del("findVegetables");
        return integer;
    }

    @Override
    public Integer deleteBatch(Integer[] ids) {
        Integer integer = vegetablesDao.deleteBatch(ids);
        redisUtil.del("findVegetables");
        return integer;
    }
}
